package com.situ.mall.controller;

import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.*;
import com.situ.mall.service.IShippingService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    private IShippingService shippingService;

    @RequestMapping("/ListByUerId")
    @ResponseBody
    public Result ListByUerId(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Shipping> list = shippingService.ListByUerId(userId);
        return Result.ok(list);
    }
    @RequestMapping("/selectProvince")
    @ResponseBody
    public Result selectProvince() {
        List<TmProvince> list = shippingService.selectProvince();
        return Result.ok(list);
    }
    @RequestMapping("/selectCity")
    @ResponseBody
    public Result selectCity(Integer provinceId) {
        List<TmCity> list = shippingService.selectCity(provinceId);
        return Result.ok(list);
    }
    @RequestMapping("/selectArea")
    @ResponseBody
    public Result selectArea(Integer cityId) {
        List<TmArea> list = shippingService.selectArea(cityId);
        return Result.ok(list);
    }
    @RequestMapping("/add")
    @ResponseBody
    public Result add(Shipping shipping, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        shipping.setUserId(userId);
        Integer provinceId = shipping.getProvinceId();
        String receiverProvince = shippingService.selectProvinceName(provinceId);
        shipping.setReceiverProvince(receiverProvince);
        Integer cityId = shipping.getCityId();
        String receiverCity = shippingService.selectCityName(cityId);
        shipping.setReceiverCity(receiverCity);
        Integer areaId = shipping.getAreaId();
        String receiverArea = shippingService.selectAreaName(areaId);
        shipping.setReceiverArea(receiverArea);
        if (!ObjectUtils.isEmpty(shipping.getChecked())) {
            Integer checked = shipping.getChecked();
            if (checked == 1) {
                shippingService.updateCheckedAll(userId);
            }
        }
        shippingService.add(shipping);
        return Result.ok("添加成功！");
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Shipping shipping) {
        shipping.setIsDeleted((byte) 1);
        shippingService.delete(shipping);
        return Result.ok("删除成功！");
    }
    @RequestMapping("/updateDefault")
    @ResponseBody
    public Result updateDefault(Shipping shipping, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        shippingService.updateCheckedAll(userId);
        shipping.setChecked(1);
        shippingService.updateDefault(shipping);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Shipping shipping = shippingService.selectById(id);
        List<TmProvince> listProvince = shippingService.selectProvince();
        Integer provinceId = shipping.getProvinceId();
        List<TmCity> listCity = shippingService.selectCity(provinceId);
        Integer cityId = shipping.getCityId();
        List<TmArea> listArea = shippingService.selectArea(cityId);
        Map<String, Object> map = new HashMap<>();
        map.put("shipping", shipping);
        map.put("listProvince", listProvince);
        map.put("listCity", listCity);
        map.put("listArea", listArea);
        return Result.ok(map);
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Shipping shipping, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        shipping.setUserId(userId);
        Integer provinceId = shipping.getProvinceId();
        String receiverProvince = shippingService.selectProvinceName(provinceId);
        shipping.setReceiverProvince(receiverProvince);
        Integer cityId = shipping.getCityId();
        String receiverCity = shippingService.selectCityName(cityId);
        shipping.setReceiverCity(receiverCity);
        Integer areaId = shipping.getAreaId();
        String receiverArea = shippingService.selectAreaName(areaId);
        Integer checked;
        shipping.setReceiverArea(receiverArea);
        if (!ObjectUtils.isEmpty(shipping.getChecked())) {
            checked = shipping.getChecked();
            if (checked == 1) {
                shippingService.updateCheckedAll(userId);
            }
        } else {
            checked = 0;
            shipping.setChecked(checked);
        }
        shippingService.update(shipping);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/shippingById")
    @ResponseBody
    public Result shippingById(Integer shippingId) {
        Shipping shipping = shippingService.shippingById(shippingId);
        return Result.ok(shipping);
    }
    @RequestMapping("/ShippingByOrderNo")
    @ResponseBody
    public Result ShippingByOrderNo(Long orderNo) {
        Shipping shipping = shippingService.ShippingByOrderNo(orderNo);
        return Result.ok(shipping);
    }
}
